// packageE/member/component/founder/echievement-list-item3/echievement-list-item3.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    datas: {
      type: null,
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    timeOver: "",
    lastTime: "00天00时00分00秒",
  },
  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {},
    moved() {},
    detached() {
      clearInterval(this.data.timeOver);
    },
  },
  observers: {
    datas: function (b) {
      let that = this;
      console.log(b);
      for (let i = 0; i < b.project.length; i++) {
        console.log(b.project);
        if (b.project[i].project_shell_type == 3) {
          let istime = Number(b.project[i].lock_update_at) * 1000;
          let time = that.formatDate(istime);
          console.log(time);
          this.data.timeOver = setInterval(() => {
            const nowDaytime = new Date().getTime();
            // let lastTime  = (istime - nowDaytime) /1000
            that._countDown(istime, nowDaytime);
          }, 1000);
          that.setData({
            nowtime: time,
          });
        }
      }
      // if (b.project == 3) {
      //   console.log(`${b.lock_update_at}000`)
      //   let istime = Number(`${b.lock_update_at}000`)
      //   let time = that.formatDate(istime)
      //   this.data.timeOver = setInterval(() => {
      //     const nowDaytime = new Date().getTime();
      //     // let lastTime  = (istime - nowDaytime) /1000
      //     that._countDown(istime, nowDaytime)
      //   }, 1000);
      //   that.setData({
      //     nowtime: time
      //   })
      //   console.log(time)
      // }
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    _countDown(istime, nowDaytime) {
      if (istime - nowDaytime < 0) {
        clearInterval(this.data.timeOver);
        this.setData({
          lastTime: "00天00时00分00秒",
        });
        return;
      }
      let time = (istime - nowDaytime) / 1000;
      // 获取天、时、分、秒
      let day = parseInt(time / (60 * 60 * 24));
      let hou = parseInt((time % (60 * 60 * 24)) / 3600);
      let min = parseInt(((time % (60 * 60 * 24)) % 3600) / 60);
      let sec = parseInt(((time % (60 * 60 * 24)) % 3600) % 60);
      let obj = {
        day: this._timeFormat(day),
        hou: this._timeFormat(hou),
        min: this._timeFormat(min),
        sec: this._timeFormat(sec),
      };
      this.setData({
        lastTime: `${obj.day}天${obj.hou}时${obj.min}分${obj.sec}秒`,
      });
    },
    //小于10的格式化函数
    _timeFormat(param) {
      //小于10的格式化函数
      return param < 10 ? "0" + param : param;
    },
    formatDate(date) {
      console.log(date);
      // var date = new Date(date);
      var YY = date.getFullYear() + "-";
      var MM =
        (date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1) + "-";
      var DD = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
      var hh =
        (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
      var mm =
        (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) +
        ":";
      var ss =
        date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
      return YY + MM + DD + " " + hh + mm + ss;
    },
  },
});
